﻿Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports log4net

Partial Class CopyVersionUser
    Inherits System.Web.UI.Page

    Public AlertText As String
    Public ScriptText As String
    Private Shared logger As ILog = LogManager.GetLogger("CopyVersionUser")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If

        'Alert(UsersWebCombo.Value)
        PlanYearsTxt.Text = Val(Session("PlanYear"))
        'If IsPostBack = False Then
        ' Alert(UsersWebCombo.Value)
        Session("usersNow") = UsersWebCombo.Value '"bb9222" 


        If UsersWebCombo.Value <> "" Then


            Dim Myversion As Integer
            Dim username As String = Session("usersNow")

            Dim top_fct As String
            Dim version As Integer
            GetTopFct(username, PlanYearsTxt.Text, 1, top_fct, version)

            ' Response.Write("top" & top_fct)
            ' Exit Sub

            Dim FCTDataSet As New DataSet
            Try
                GetChildWithVersion(top_fct, PlanYearsTxt.Text, 1, FCTDataSet, Myversion)

            Catch ex As Exception
                Alert("ไม่มีหน่วยงานลูก")
                Exit Sub
            End Try

            Dim adap As New FundCenter2TableAdapters.fundCenterDetailTableAdapter
            Dim nowVersion As Integer = adap.getNowVersion(top_fct, PlanYearsTxt.Text)
            If FCTDataSet.Tables(0).Rows.Count < 2 Then
                ' Response.Redirect("Default.aspx")
                Alert("ไม่มีหน่วยงานลูก")
            End If

            If nowVersion > Myversion Then
                coppy_btn.Visible = False
                ForceCopyBtn.Visible = False
            End If
            HdUser.Value = Session("usersNow")
            HdMyVersion.Value = Myversion
            WebGrid1.DataSource = FCTDataSet.Tables(0)
        End If
        ' End If


    End Sub

    Sub GetChildWithVersion(FundCenterCode As String, PlanYear As Decimal, FctGroup As String, ByRef ds As DataSet, ByRef CopyToVersion As Integer)


        Dim dt As DataTable = ds.Tables.Add("Items")
        dt.Columns.Add("FundCenterCode", Type.GetType("System.String"))
        dt.Columns.Add("FundCenterName", Type.GetType("System.String"))
        dt.Columns.Add("now_version", Type.GetType("System.Int32"))
        dt.Columns.Add("level", Type.GetType("System.Int32"))

        dt.Columns.Add("CreateDate", Type.GetType("System.String"))
        dt.Columns.Add("ReleaseFlag", Type.GetType("System.String"))
        Dim Radapter As New FundCenterRelationTableAdapters.FundCenterReleasesTableAdapter
        Dim Rdatatable As New FundCenterRelation.FundCenterReleasesDataTable
        Dim Fadapter As New FundCenterRelationTableAdapters.FundCenterHierarchiesTableAdapter
        Dim Fdatatable As New FundCenterRelationTableAdapters.FundCenterHierarchiesTableAdapter
        Dim Hadapter As New FundCenterRelationTableAdapters.FctHTableAdapter
        Dim Hdatatable As New FundCenterRelation.FctHDataTable
        Dim Cdatatable As New FundCenterRelation.FctHDataTable
        Dim Badapter As New FundCenterRelationTableAdapters.BudgetDetailsTableAdapter

        Hdatatable = Hadapter.GetDataByFctCode(FundCenterCode, PlanYear, FctGroup)
        Dim RootGroupPath As String = ""

        If Hdatatable.Count = 0 Then
            Exit Sub
        End If
        Dim r As DataRow
        'add root
        For Each Hrow As FundCenterRelation.FctHRow In Hdatatable
            RootGroupPath = Hrow.grouppath
            r = dt.NewRow()
            r("FundCenterCode") = Hrow.FundCenter_FundCenterCode
            r("FundCenterName") = Hrow.Description
            r("level") = Val(Left(Fadapter.GetOrgLevel(Hrow.FundCenter_FundCenterCode, PlanYear, FctGroup), 1))
            CopyToVersion = r("level")
            r("now_version") = 1
            Rdatatable = Radapter.GetDataByFCT(r("FundCenterCode"), PlanYear, r("now_version"))

            r("CreateDate") = ""
            r("ReleaseFlag") = ""
            For Each rrow As FundCenterRelation.FundCenterReleasesRow In Rdatatable
                r("CreateDate") = rrow.CreatedDate
                r("ReleaseFlag") = rrow.ReleaseFlag
            Next
            If r("ReleaseFlag") = "" Then
                If Badapter.CountRecord(r("FundCenterCode"), PlanYear, r("now_version")) > 0 Then
                    r("ReleaseFlag") = "N"
                End If
            End If
            dt.Rows.Add(r)
        Next
        'get child 1 level
        Hdatatable = Hadapter.GetDataByGroupPath(RootGroupPath & "__", PlanYear, FctGroup)
        Dim version As Integer
        For Each row As FundCenterRelation.FctHRow In Hdatatable
            r = dt.NewRow()
            r("FundCenterCode") = row.FundCenter_FundCenterCode
            r("FundCenterName") = row.Description
            r("level") = Val(Left(Fadapter.GetOrgLevel(row.FundCenter_FundCenterCode, PlanYear, FctGroup), 1))
            'get child all
            Cdatatable = Hadapter.GetDataByGroupPath(row.grouppath & "%", PlanYear, FctGroup)
            If Cdatatable.Count = 0 Then
                r("now_version") = 1
            Else
                r("now_version") = r("level")
            End If
            version = r("now_version")
            Rdatatable = Radapter.GetDataByFCT(r("FundCenterCode"), PlanYear, r("now_version"))

            r("CreateDate") = ""
            r("ReleaseFlag") = ""
            For Each rrow As FundCenterRelation.FundCenterReleasesRow In Rdatatable
                r("CreateDate") = rrow.CreatedDate
                r("ReleaseFlag") = rrow.ReleaseFlag
            Next
            If r("ReleaseFlag") = "" Then
                If Badapter.CountRecord(r("FundCenterCode"), PlanYear, r("now_version")) > 0 Then
                    r("ReleaseFlag") = "N"
                End If
            End If

            dt.Rows.Add(r)
            'insert child
            For Each crow As FundCenterRelation.FctHRow In Cdatatable
                r = dt.NewRow()
                r("FundCenterCode") = crow.FundCenter_FundCenterCode
                r("FundCenterName") = crow.Description

                r("level") = Val(Left(Fadapter.GetOrgLevel(crow.FundCenter_FundCenterCode, PlanYear, FctGroup), 1))

                r("now_version") = version

                Rdatatable = Radapter.GetDataByFCT(r("FundCenterCode"), PlanYear, r("now_version"))

                r("CreateDate") = ""
                r("ReleaseFlag") = ""
                For Each rrow As FundCenterRelation.FundCenterReleasesRow In Rdatatable
                    r("CreateDate") = rrow.CreatedDate
                    r("ReleaseFlag") = rrow.ReleaseFlag
                Next
                If r("ReleaseFlag") = "" Then
                    If Badapter.CountRecord(r("FundCenterCode"), PlanYear, r("now_version")) > 0 Then
                        r("ReleaseFlag") = "N"
                    End If
                End If
                dt.Rows.Add(r)
            Next



        Next

    End Sub

    Sub GetTopFct(username As String, PlanYear As Decimal, FctGroup As String, ByRef fctCode As String, ByRef version As Integer)
        Dim adapter As New FundCenter2TableAdapters.FundCenterHierarchiesTableAdapter
        Dim datatable As New FundCenter2.FundCenterHierarchiesDataTable
        datatable = adapter.GetTopFctByUser(username, PlanYear, FctGroup)

        Try
            For Each row As FundCenter2.FundCenterHierarchiesRow In datatable
                fctCode = row.FundCenter_FundCenterCode
                Dim level As Integer = row.Level
                If level = 1 Then
                    version = 5
                ElseIf level = 2 Then
                    version = 4
                ElseIf level = 3 Then
                    version = 3
                ElseIf level = 4 Then
                    version = 2
                Else
                    version = 1

                End If

            Next

        Catch ex As Exception
            fctCode = ""
            version = 1
        End Try


    End Sub

    'Sub GetChildWithVersion(FundCenterCode As String, PlanYear As Decimal, FctGroup As String, ByRef ds As DataSet, ByRef version As Integer)


    '    Dim dt As DataTable = ds.Tables.Add("Items")
    '    dt.Columns.Add("FundCenterCode", Type.GetType("System.String"))
    '    'dt.Columns.Add("version", Type.GetType("System.Int32"))
    '    dt.Columns.Add("level", Type.GetType("System.Int32"))
    '    dt.Columns.Add("FundCenterName", Type.GetType("System.String"))
    '    dt.Columns.Add("CreateDate", Type.GetType("System.String"))
    '    dt.Columns.Add("ReleaseFlag", Type.GetType("System.String"))
    '    dt.Columns.Add("Now_version", Type.GetType("System.Int32"))
    '    Dim adapter As New FundCenter2TableAdapters.FundCenterHierarchiesTableAdapter
    '    Dim datatable As New FundCenter2.FundCenterHierarchiesDataTable
    '    Dim Myversion As Integer = 1

    '    Dim r As DataRow
    '    r = dt.NewRow()
    '    r("FundCenterCode") = FundCenterCode
    '    r("Now_version") = 1

    '    Dim level As Integer

    '    level = adapter.GetLevel(FundCenterCode, PlanYear, FctGroup) / 10
    '    If level = 0 Then
    '        level = adapter.GetLevelByChild(FundCenterCode, PlanYear, FctGroup) / 10
    '    End If

    '    r("Level") = level
    '    HdMyVersion.Value = level
    '    version = level
    '    ' Response.Write(FundCenterCode & PlanYear & version)
    '    'Response.Write(HdMyVersion.Value & "/" & level)
    '    Dim fadapter As New FundCenter2TableAdapters.FundCentersTableAdapter
    '    r("FundCenterName") = fadapter.GetName(FundCenterCode, PlanYear)
    '    Dim adap2 As New FundCenter2TableAdapters.fundCenterDetailTableAdapter
    '    For Each row As FundCenter2.fundCenterDetailRow In adap2.GetDataFundCenter(FundCenterCode, PlanYear, Myversion)

    '        r("CreateDate") = row.CreatedDate.ToLongDateString
    '        Try
    '            r("ReleaseFlag") = row.ReleaseFlag
    '        Catch ex As Exception
    '            r("ReleaseFlag") = "N"
    '        End Try

    '        'r("Now_version") = row.Version
    '    Next



    '    dt.Rows.Add(r)

    '    datatable = adapter.GetDataByParent(FundCenterCode, PlanYear, FctGroup)
    '    For Each row As FundCenter2.FundCenterHierarchiesRow In datatable
    '        GetChild(FundCenterCode, row.FundCenter_FundCenterCode, ds, dt, PlanYear, FctGroup, version)
    '    Next




    'End Sub
    'Sub GetChild(top_code As String, ByVal parent_id As String, ByRef ds As DataSet, ByRef dt As DataTable, PlanYear As Decimal, FctGroup As String, parent_version As Decimal)

    '    Dim adapter As New FundCenter2TableAdapters.FundCenterHierarchiesTableAdapter
    '    Dim datatable As New FundCenter2.FundCenterHierarchiesDataTable
    '    datatable = adapter.GetDataByParent(parent_id, PlanYear, FctGroup)
    '    Dim ChildAmount As Decimal
    '    Dim Relate As Decimal
    '    Dim r As DataRow
    '    Dim version As Integer = 1
    '    r = dt.NewRow()
    '    r("FundCenterCode") = parent_id

    '    ChildAmount = adapter.CountChild(parent_id, PlanYear, FctGroup)
    '    Relate = adapter.CountRelate(top_code, parent_id, PlanYear, FctGroup)
    '    If ChildAmount = 0 And Relate > 0 Then
    '        r("Now_version") = 1
    '    Else
    '        version = parent_version - 1
    '        r("Now_version") = version


    '    End If
    '    Dim level As Integer

    '    level = adapter.GetLevel(parent_id, PlanYear, FctGroup) / 10
    '    If level = 0 Then
    '        level = adapter.GetLevelByChild(parent_id, PlanYear, FctGroup) / 10
    '    End If
    '    Dim fadapter As New FundCenter2TableAdapters.FundCentersTableAdapter
    '    r("FundCenterName") = fadapter.GetName(parent_id, PlanYear)
    '    r("level") = level
    '    'Response.Write(level)
    '    Dim adap2 As New FundCenter2TableAdapters.fundCenterDetailTableAdapter
    '    For Each row As FundCenter2.fundCenterDetailRow In adap2.GetDataFundCenter(parent_id, PlanYear, version)

    '        r("CreateDate") = row.CreatedDate.ToLongDateString
    '        Try
    '            r("ReleaseFlag") = row.ReleaseFlag
    '        Catch ex As Exception
    '            r("ReleaseFlag") = "N"
    '        End Try
    '    Next

    '    dt.Rows.Add(r)

    '    For Each row As FundCenter2.FundCenterHierarchiesRow In datatable
    '        GetChild(top_code, row.FundCenter_FundCenterCode, ds, dt, PlanYear, FctGroup, parent_version)
    '    Next
    'End Sub


    Protected Sub WebGrid1_InitializeDataSource(sender As Object, e As DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        'Dim Myversion As Integer
        'Dim username As String = Session("users")

        'Dim top_fct As String
        'top_fct = GetTopFct(username, PlanYearsTxt.Text, 1)
        'Dim FCTDataSet As New DataSet
        'GetChildWithVersion(top_fct, PlanYearsTxt.Text, 1, FCTDataSet, Myversion)
        'HdMyVersion.Value = Myversion
        'WebGrid1.DataSource = FCTDataSet.Tables(0)
    End Sub

    Protected Sub WebGrid1_InitializePostBack(sender As Object, e As PostbackEventArgs) Handles WebGrid1.InitializePostBack

        If e.Action = PostBackAction.Custom Then
            Dim ActionType As String = TryCast(Request("type"), String)
            If ActionType = "1" Then
                'WebGrid1.ClientAction.Alert("post1")
                'WebGrid1.ClientAction.Refresh()
                'Exit Sub

                Dim dd As New DataTable
                Try
                    dd = WebGrid1.DataSource
                    If dd.Rows.Count = 0 Then
                        WebGrid1.ClientAction.Alert("ไม่มีหน่วยงานที่จะ copy version ได้")
                        Exit Sub
                    End If
                Catch ex As Exception
                    logger.Error(ex.Message)
                    logger.Error(ex.StackTrace)

                    WebGrid1.ClientAction.Alert("ไม่มีหน่วยงานที่จะ copy version ได้")
                    Exit Sub
                End Try



                Dim count As Integer = 0
                For Each row As DataRow In dd.Rows
                    ' WebGrid1.ClientAction.Alert(row.Item("FundCenterCode"))
                    Try
                        If row.Item("ReleaseFlag") = "N" Then
                            count = count + 1
                        End If
                    Catch ex As Exception
                        count = count + 1
                    End Try

                Next
                If count > 1 Then
                    WebGrid1.ClientAction.Alert("มี " & count & "หน่วยงาน ยังไม่ได้ส่งแผน ไม่สามารถ Copy Version ได้ในขณะนี้")
                    Exit Sub

                End If


                Dim adapter As New FundCenter2TableAdapters.BudgetDetailsTableAdapter
                Dim dtrow As New FundCenter2.BudgetDetailsDataTable
                Dim adapAt As New FundCenter2TableAdapters.AttachmentsTableAdapter
                Dim adapRev As New FundCenter2TableAdapters.BudgetRevPlansTableAdapter
                Dim dtRev As New FundCenter2.BudgetRevPlansDataTable
                Dim adapBud12 As New FundCenter2TableAdapters.Budget12TableAdapter
                Dim Badapter As New FundCenterRelationTableAdapters.BudgetDetailsTableAdapter
                Dim newVersion As Integer = HdMyVersion.Value
                Dim countF1 As Integer = 0
                For Each row As DataRow In dd.Rows ' ,set FundCenterRelease
                    Dim chBudg As Integer = 0
                    chBudg = Badapter.CountRecord(row.Item("FundCenterCode"), PlanYearsTxt.Text, row.Item("Now_version"))

                    If chBudg = 0 Then

                        dtrow = adapter.GetBudget_id(row.Item("FundCenterCode"), PlanYearsTxt.Text, row.Item("Now_version"))
                        'Response.Write(row.Item("FundCenterCode") & PlanYearsTxt.Text & row.Item("Now_version") & "<BR>")
                        For Each rowId As FundCenter2.BudgetDetailsRow In dtrow    'copy budget,copy attatment
                            Dim newId As Integer = adapter.copyVersion(newVersion, rowId.Id)
                            adapAt.copyAttachment(newId, rowId.Id)
                            adapBud12.copyBudget12(newVersion, newId, rowId.Id)

                        Next

                        dtRev = adapRev.GetDataBy_Func(row.Item("FundCenterCode"), PlanYearsTxt.Text, row.Item("Now_version"))
                        For Each rowIdR As FundCenter2.BudgetRevPlansRow In dtRev    'copy budget,copy attatment
                            adapRev.copyRevPlan(newVersion, rowIdR.Id)

                        Next

                        'Exit Sub
                        Dim exist As Integer
                        Dim adapRel As New FundCenter2TableAdapters.FundCenterReleasesTableAdapter
                        Dim adap As New checkPlanDsTableAdapters.FundCenterReleasesTableAdapter

                        exist = adap.checkExist(row.Item("FundCenterCode"), Val(PlanYearsTxt.Text), newVersion)
                        If exist = 0 Then
                            Dim preVer As String = row.Item("Now_version")
                            If Trim(row.Item("ReleaseFlag")) = "" Then
                                preVer = "0"
                            End If
                            'Dim existN As Integer
                            'existN = adap.checkExist(row.Item("FundCenterCode"), Val(PlanYearsTxt.Text), row.Item("Now_version"))
                            If countF1 = 0 Then
                                adapRel.InsertRelease(PlanYearsTxt.Text, "Y", "N", row.Item("Now_version"), "0", Date.Now, HdUser.Value, row.Item("FundCenterCode"), PlanYearsTxt.Text)
                                preVer = row.Item("Now_version")
                            End If
                            adapRel.InsertRelease(PlanYearsTxt.Text, "N", "Y", newVersion, preVer, Date.Now, HdUser.Value, row.Item("FundCenterCode"), PlanYearsTxt.Text)
                        Else
                            adap.UpdateSendPlan("N", Date.Now, Session("users"), "Y", newVersion, row.Item("FundCenterCode"), Val(PlanYearsTxt.Text))
                        End If
                        adapRel.UpdateOldRelease("N", Date.Now, Session("users"), row.Item("FundCenterCode"), PlanYearsTxt.Text, row.Item("Now_version"))
                    End If
                    countF1 += 1
                Next

                WebGrid1.ClientAction.Alert("Copy Version เรียบร้อยแล้ว")
            ElseIf ActionType = "2" Then
                'WebGrid1.ClientAction.Alert("post2")

                'WebGrid1.ClientAction.Refresh()
                'Exit Sub



                Dim dd As New DataTable
                Try
                    dd = WebGrid1.DataSource
                    If dd.Rows.Count = 0 Then
                        WebGrid1.ClientAction.Alert("ไม่มีหน่วยงานที่จะ copy version ได้")
                        Exit Sub
                    End If
                Catch ex As Exception
                    WebGrid1.ClientAction.Alert("ไม่มีหน่วยงานที่จะ copy version ได้")
                    Exit Sub
                End Try
                Dim count As Integer = 0
                Dim adapter As New FundCenter2TableAdapters.BudgetDetailsTableAdapter
                Dim dtrow As New FundCenter2.BudgetDetailsDataTable
                Dim adapAt As New FundCenter2TableAdapters.AttachmentsTableAdapter
                Dim adapBud12 As New FundCenter2TableAdapters.Budget12TableAdapter
                Dim Badapter As New FundCenterRelationTableAdapters.BudgetDetailsTableAdapter
                Dim newVersion As Integer = HdMyVersion.Value
                Dim countPln As Integer = 0
                'Dim dataCheck As FundCenter2.BudgetDetailsDataTable
                'For Each chrow As DataRow In dd.Rows
                '    dataCheck = adapter.CheckBudget(chrow.Item("FundCenterCode"), chrow.Item("Now_version"), Val(PlanYearsTxt.Text))
                '    If dataCheck.Count = 0 Then
                '        countPln = countPln + 1
                '    End If
                'Next
                'If countPln > 0 Then
                '    WebGrid1.ClientAction.Alert("มี " & countPln & " หน่วยงาน ที่ยังไม่ได้สร้างแผน")
                '    Exit Sub
                'End If
                Dim countF1 As Integer = 0
                For Each row As DataRow In dd.Rows
                    ' MsgBox(row.Item("ReleaseFlag"))

                    Dim chBudg As Integer = 0
                    chBudg = Badapter.CountRecord(row.Item("FundCenterCode"), PlanYearsTxt.Text, row.Item("Now_version"))

                    If chBudg = 0 Then
                        Dim ReleaseF As String
                        Try
                            ReleaseF = row.Item("ReleaseFlag")
                        Catch ex As Exception
                            ReleaseF = "N"
                        End Try

                        Dim adapRel As New FundCenter2TableAdapters.FundCenterReleasesTableAdapter
                        Dim adap2 As New checkPlanDsTableAdapters.FundCenterReleasesTableAdapter
                        Dim exist As Integer
                        exist = adap2.checkExist(row.Item("FundCenterCode"), Val(PlanYearsTxt.Text), newVersion)
                        'Dim exist2 As Integer
                        'If Val(row.Item("Now_version")) = 1 Then
                        '    exist2 = adap2.checkExist(row.Item("FundCenterCode"), Val(PlanYearsTxt.Text), row.Item("Now_version"))
                        '    If exist = 0 Then
                        '        adapRel.InsertRelease(PlanYearsTxt.Text, "Y", "N", 1, 0, Date.Now, HdUser.Value, row.Item("FundCenterCode"), Val(PlanYearsTxt.Text))
                        '    End If
                        'End If
                        If ReleaseF <> "Y" Then
                            Dim causeForce As String = HdCause.Value
                            Dim adap As New checkPlanDsTableAdapters.FundCenterReleasesTableAdapter
                            adap.UpdateForceFlag("Y", Date.Now, Session("users"), 1, causeForce, row.Item("FundCenterCode"), PlanYearsTxt.Text, row.Item("Now_version"))
                        End If
                        dtrow = adapter.GetBudget_id(row.Item("FundCenterCode"), PlanYearsTxt.Text, row.Item("Now_version"))

                        For Each rowId As FundCenter2.BudgetDetailsRow In dtrow    'copy budget,copy attatment,set FundCenterRelease
                            Dim newId As Integer = adapter.copyVersion(newVersion, rowId.Id)
                            adapAt.copyAttachment(newId, rowId.Id)
                            adapBud12.copyBudget12(newVersion, newId, rowId.Id)
                        Next

                        Dim adapRev As New FundCenter2TableAdapters.BudgetRevPlansTableAdapter
                        Dim dtRev As New FundCenter2.BudgetRevPlansDataTable
                        dtRev = adapRev.GetDataBy_Func(row.Item("FundCenterCode"), PlanYearsTxt.Text, row.Item("Now_version"))
                        For Each rowIdR As FundCenter2.BudgetRevPlansRow In dtRev    'copy budget,copy attatment
                            adapRev.copyRevPlan(newVersion, rowIdR.Id)

                        Next


                        If exist = 0 Then
                            Dim preVer As String = row.Item("Now_version")
                            If Trim(row.Item("ReleaseFlag")) = "" Then
                                preVer = "0"
                            End If
                            If countF1 = 0 Then
                                adapRel.InsertRelease(PlanYearsTxt.Text, "Y", "N", row.Item("Now_version"), "0", Date.Now, HdUser.Value, row.Item("FundCenterCode"), PlanYearsTxt.Text)
                                preVer = row.Item("Now_version")
                            End If
                            adapRel.InsertRelease(PlanYearsTxt.Text, "N", "Y", newVersion, preVer, Date.Now, HdUser.Value, row.Item("FundCenterCode"), Val(PlanYearsTxt.Text))

                        Else
                            adap2.UpdateSendPlan("N", Date.Now, Session("users"), "Y", newVersion, row.Item("FundCenterCode"), Val(PlanYearsTxt.Text))
                        End If
                        adapRel.UpdateOldRelease("N", Date.Now, Session("users"), row.Item("FundCenterCode"), PlanYearsTxt.Text, row.Item("Now_version"))
                    End If
                    countF1 += 1
                Next


                WebGrid1.ClientAction.Alert("Force Copy Version เรียบร้อยแล้ว")

            Else


                Dim data As String = TryCast(Request("data"), String)

                'WebGrid1.ClientAction.Alert(data)
                Dim dd = data.Split(",")
                Dim fundCen As String = dd(0)
                Dim version As String = dd(1)
                Dim countr As Integer = 0
                Dim adapr As New FundCenter2TableAdapters.FundCenterReleasesTableAdapter
                countr = adapr.checkCopy(fundCen, HdMyVersion.Value, Val(Session("PlanYear")))

                If countr > 0 Then
                    Dim adapre As New FundCenter2TableAdapters.FundCenterReleasesTableAdapter
                    Dim preVersion As Integer = adapre.getPreVersion(fundCen, HdMyVersion.Value, Val(Session("PlanYear")))

                    'delete  budgetDetail,budget12,attachment,RevPlan,FundCenterRelease 

                    Dim dtrow As New FundCenter2.BudgetDetailsDataTable
                    Dim adapter As New FundCenter2TableAdapters.BudgetDetailsTableAdapter
                    dtrow = adapter.GetBudget_id(fundCen, Val(Session("PlanYear")), HdMyVersion.Value)
                    'Response.Write(row.Item("FundCenterCode") & PlanYearsTxt.Text & row.Item("Now_version") & "<BR>")
                    Dim adapAt As New FundCenter2TableAdapters.AttachmentsTableAdapter

                    For Each rowId As FundCenter2.BudgetDetailsRow In dtrow    'copy budget,copy attatment
                        adapAt.DeleteReleaseAtt(rowId.Id)

                    Next
                    adapter.DeleteReleaseBud(fundCen, HdMyVersion.Value, Val(Session("PlanYear")))
                    Dim adapRev As New FundCenter2TableAdapters.BudgetRevPlansTableAdapter
                    adapRev.DeleteReleaseRev(fundCen, HdMyVersion.Value, Val(Session("PlanYear")))
                    Dim adapBud12 As New FundCenter2TableAdapters.Budget12TableAdapter
                    adapBud12.DeleteRelease(fundCen, HdMyVersion.Value, Val(Session("PlanYear")))

                    adapre.DeleteReleaseFun(fundCen, HdMyVersion.Value, Val(Session("PlanYear")))
                    adapre.ReleaseBackVersion("N", "Y", Session("users"), Date.Now, fundCen, Val(Session("PlanYear")), preVersion)

                    WebGrid1.ClientAction.Alert("Release หน่วยงานนี้เรียบร้อยแล้ว")
                Else
                    WebGrid1.ClientAction.Alert("ไม่สามารถ Release หน่วยงานนี้ได้")
                End If



            End If

            WebGrid1.ClientAction.Refresh()
        End If
    End Sub

    Protected Sub WebGrid1_InitializeRow(sender As Object, e As RowEventArgs) Handles WebGrid1.InitializeRow
        Dim imag As New Image
        imag = CType(e.Row.Cells.GetNamedItem("FundCenter").GetTemplateContainer().FindControl("Image1"), Image)
        Try
            If e.Row.GetDataRow("ReleaseFlag") = "Y" Then
                imag.ToolTip = "ส่งแผนแล้ว"
                imag.ImageUrl = "Images/planIc/sendIc.png"
            ElseIf e.Row.GetDataRow("ReleaseFlag") = "N" Then
                imag.ImageUrl = "Images/planIc/nosend.png"
                imag.ToolTip = "ยังไม่ได้ส่งแผน"
            Else
                imag.ImageUrl = "Images/planIc/nobuild.png"
                imag.ToolTip = "ยังไม่ได้สร้างแผน"
            End If
        Catch ex As Exception
            imag.ImageUrl = "Images/planIc/nobuild.png"
        End Try


        Dim lb As New Label
        lb = CType(e.Row.Cells.GetNamedItem("FundCenter").GetTemplateContainer().FindControl("Label2"), Label)
        Dim shif As String = ""
        For i As Integer = 1 To Val(HdMyVersion.Value) - Val(e.Row.GetDataRow("level"))
            shif = shif & "--"
        Next
        lb.Text = shif
        ' MsgBox(HdMyVersion.Value & "/" & e.Row.GetDataRow("Now_version"))
        Try

            If HdMyVersion.Value = 1 Then
                Dim imag2 As New Image
                imag2 = CType(e.Row.Cells.GetNamedItem("Release").GetTemplateContainer().FindControl("Image1"), Image)
                imag2.Visible = False

            Else
                Dim countr As Integer = 0
                Dim adapr As New FundCenter2TableAdapters.FundCenterReleasesTableAdapter
                countr = adapr.checkCopy(e.Row.GetDataRow("FundCenterCode"), HdMyVersion.Value, Val(Session("PlanYear")))

                Dim imag2 As New Image
                imag2 = CType(e.Row.Cells.GetNamedItem("Release").GetTemplateContainer().FindControl("Image1"), Image)

                If countr > 0 Then

                    imag2.Visible = True
                Else
                    imag2.Visible = False
                End If


            End If
        Catch ex As Exception

        End Try


    End Sub
    Protected Sub UsersWebCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles UsersWebCombo.InitializeDataSource

        Dim adap As New MasterDataSetTableAdapters.Users2TableAdapter
        Dim dt As New MasterDataSet.Users2DataTable
        dt = adap.GetDataByPlanYear2(Session("PlanYear"))
        e.DataSource = dt

    End Sub

    Sub Alert(ByVal message As String)
        AlertText = "<script> alert('" & message & "');</script>"
    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click

        'Alert(UsersWebCombo.Value)
        'PlanYearsTxt.Text = "3333"

        '  Session("usersNow") = UsersWebCombo.Value



    End Sub
End Class
